var pnum = "";   
var initData = function(num){
       var param = {
       		template_id:template_id,
            page_num:num,
            page_size:"10"
       }
		$.ajax({
	        url: base + "/dataCenter/businessdata/templateVariableData",
	        data: param,
	        success: function (data, textStatus, jqXHR) {
	        	var pageCount = data.pager.pageCount;
	        	var dataObj ={
	        			"list":data.list,
	         			"dataTool":{
	         				"title":{
	          			      	"variable_name": "变量名称"
	          			      	,"variable_table_name": "变量取值表名"
	          			      	,"table_field_name":"取值字段名"
	          			      	,"conditions":"取值条件"
	          			      	,"variable_type":"变量类型"
								,"word_type":"文档数据类型"
	          			      	,"description":"变量描述"
	          			      	,"bz":"备注"
	          			      	,"operation":"操作"
	            			},
                            "stateData":[{"stateField":"word_type","stateArray":[{DataRegion:"书签"},{DataTag:"Tag标签"}]}],
	            			"titleCheckbox":false,
							"btn":[{"event":"updateObj","name":"编辑"},
							       {"event":"delObj","name":"删除"}]
						}
		  		};
	        	showPageTable('view', dataObj,num,pageCount,"initData");
	        },
	        error: function () {
	        }
	    });
   }

   var go_back = function(){
	   var url = base + "/dataCenter/designTemplateLibrary?name=合成模板库";
	   $.get(url, function(result){
		   $("#content").html("");
		   $("#content").html(result);
	   });
   }
   //删除变量
   var delObj = function(obj){
	   layer.confirm('确认删除?',{btn : [ '确定', '取消' ]},function(index){
		   layer.close(index);
		   $.ajax({
			   url:base+"/dataCenter/businessdata/delTemplateVariable/"+obj.id,
			   data:{},
			   success:function(data,status){
				    if(data.statusCode == 0){
						layer.msg(data.message, {
							icon : 1
						});
				    }else{
						layer.msg(data.message, {
							icon : 5
						});
				    }
				    initData(pnum);
			   }
		   });
	   });
   }
   
   var updateObj = function(obj){
		var contentDiv = function(){
			var content;
			var data={
					"title":{
						    "id":"id",
							"variable_name": "变量名称"
							,"variable_table_name": "变量取值表名"
							,"table_field_name":"取值字段名"
							,"conditions":"取值条件"
							,"variable_type":"变量类型"
                        	,"word_type":"文档数据类型"
							,"description":"变量描述"
							,"bz":"备注"
					},"text":{
						    "id":obj.id
							,"variable_name": obj.variable_name
							,"variable_table_name": obj.variable_table_name
							,"table_field_name":obj.table_field_name
							,"conditions":obj.conditions
							,"variable_type":obj.variable_type
                    		,"word_type":obj.word_type
							,"description":obj.description
							,"bz":obj.bz
					},"input":{
						    "id":"hidden"
						    ,"variable_name":"text"
						    ,"variable_table_name":"text"
						    ,"table_field_name":"text"
						    ,"conditions":"text"
							,"variable_type":"text"
                    		,"word_type":"text"
							,"description":"text"
							,"bz":"text"
					},"input_init":{
					}
			};
			var getDiv =  dc_update.innerHTML;
			laytpl(getDiv).render(data, function(html){
				content = html;
			}); 
			return content;
		}
		layopen(contentDiv());
   }
   
   //添加合成模板资料
   var addData = function(){
		var contentDiv = function(){
			var content;
			var data={
					"title":{
							"variable_name": "变量名称"
							,"variable_table_name": "变量取值表名"
							,"table_field_name":"取值字段名"
							,"conditions":"取值条件"
							,"variable_type":"变量类型"
                        	,"word_type":"文档数据类型"
							,"description":"变量描述"
							,"bz":"备注"
					},"text":{
						    "variable_name":""
						    ,"variable_table_name":""
						    ,"table_field_name":""
						    ,"conditions":""
							,"variable_type":""
							,"word_type":""
							,"description":""
							,"bz":""
					},"input":{
						    "variable_name":"text"
						    ,"variable_table_name":"select"
						    ,"table_field_name":"new_select"
						    ,"conditions":"text"
							,"variable_type":"select3"
							,"word_type":"select2"
							,"description":"text"
							,"bz":"text"
					},"input_init":{
					}
			};
			var getDiv =  dc_update.innerHTML;
			laytpl(getDiv).render(data, function(html){
				content = html;
			}); 
			return content;
		}
		layopen(contentDiv());
       initTables();//初始化提入资料和设计单元表
       initField();//初始化表字段
       initVariableType();//初始化变量类型 现只有 varchar
	   initWordType();//初始化 文档数据类型
       chanageTableSelect();
   }

   var initTables = function(){
       $.post(base+"/dataCenter/businessdata/getTables",{},
           function(data,status){
               if(data != null && data != null){
                   var optionstring = "";
                   $.each(data, function(i,item){
                       optionstring += "<option value=\"" + item.TABLE_NAME + "\" >" + item.TABLE_NAME + "</option>";
                   });
                   $("select[name='variable_table_name']").html('<option value=""></option>' + optionstring);
               }else{
                   $("select[name='variable_table_name']").html('<option value=""></option>');
               }
               form.render();
           });
   }

   var initField = function(tableId){
       $.post(base+"/dataCenter/businessdata/getFieldByTableId/"+tableId,{},
           function(data,status){
               if(data!=''){
                   var optionstring = "";
                   $.each(data, function(i,item){
                       optionstring += "<option value=\"" + item.COLUMN_NAME + "\" >" + item.COLUMN_COMMENT + "</option>";
                   });
                   $("select[name='table_field_name']").html('<option value=""></option>' + optionstring);
               }else{
                   $("select[name='table_field_name']").html('<option value=""></option>');
               }
               form.render();
           });
   }

   //监听专业下拉选
   var chanageTableSelect = function(){
       form.on("select(select)",function(value){
           var tableId = value.value;
           initField(tableId);
       });
   }

   var initVariableType = function(){
       var optionstring = "";
       optionstring += "<option value='varchar'>字符串</option>";
       //optionstring += "<option value='List'>Tag标签</option>";
       $("select[name='variable_type']").html('<option value=""></option>' + optionstring);
       form.render();
   }

   var initWordType = function(){
	   var optionstring = "";
	   optionstring += "<option value='DataRegion'>书签</option>";
       optionstring += "<option value='DataTag'>Tag标签</option>";
       optionstring += "<option value='Image'>图片</option>";
       optionstring += "<option value='InputTable'>提入资料表格</option>";
       optionstring += "<option value='DesignTable'>设计单元表格</option>";
	   $("select[name='word_type']").html('<option value=""></option>' + optionstring);
	   form.render();
   }

   //渲染弹窗
   var layopen = function(content){
		layer.open({
		  	title:'更新模板变量'
			,content: content
	    	,area: ['800px', '600px']
		  	,btn: ['确认', '取消']
			,yes: function(index,layero){ //确认
  		  	    var data = $(layero).find("form").serializeJSON();
  		  	    if(!verify_data(data)){
  		  	    	return false;
  		  	    }
				$.post(base+"/dataCenter/businessdata/updateTemplateVariable",{
					id:data.id==null?"":data.id,
					template_id:template_id,
                        variable_name:$.trim(data.variable_name),
                        variable_table_name:$.trim(data.variable_table_name),
                        table_field_name:$.trim(data.table_field_name),
                        conditions:data.conditions,
                        variable_type:data.variable_type,
						word_type:data.word_type,
                        description:data.description,
						bz:data.bz
				},
		  		function (data,status){
					if(data!=""&&data!=null){
						if(data.statusCode == 0){
							layer.msg('更新成功', {icon: 1,time:2000});
						}else{
							layer.msg(data.message, {icon: 2,time:2000});
						}
					}else{
						layer.msg('更新失败!', {icon: 2,time:2000});
					}
					initData(pnum);
		  		 })
			}
	});
  }

   //校验数据
   var verify_data = function(data){
		if(data.variable_name == ""){
			var obj = $("input[name='variable_name']");
			obj.focus();
			layer.tips('请填写此字段!', obj);
			return false;
		}
		if(data.variable_table_name == ""){
			var obj = $("input[name='variable_table_name']");
			obj.focus();
			layer.tips('请填写此字段!', obj);
			return false;
		}
       if(data.table_field_name == ""){
           var obj = $("input[name='table_field_name']");
           obj.focus();
           layer.tips('请填写此字段!', obj);
           return false;
       }
       if(data.variable_type == ""){
           var obj = $("input[name='variable_type']");
           obj.focus();
           layer.tips('请填写此字段!', obj);
           return false;
       }
       if(data.word_type == ""){
           var obj = $("input[name='word_type']");
           obj.focus();
           layer.tips('请填写此字段!', obj);
           return false;
       }

		return true;
   }

   $(document).ready(function(){
	   initData(1);
       $("#addData").click(addData);
   });